package com.example.module1;

import android.content.Context;
import androidx.annotation.NonNull;
import com.appinit.lib.IAppInitial;
import com.appinit.lib.annotation.Initial;

/**
 * @author GYL
 * @dateTime 2020/5/19-17:02
 * @description
 */
@Initial(priority = 100)
public class AppInitLib implements IAppInitial {
  private Context mContext;
  
  private static volatile AppInitLib sAppInitLib;
  
  /**
   * 初化调用，单例时不能调用
   * @param context
   */
  @Override
  public void onInit(@NonNull Context context) {
    this.mContext=context;
    setSingletonInstance(this);
  }
  
 synchronized void setSingletonInstance(AppInitLib appInitLib) {
    synchronized (AppInitLib.class) {
      if (sAppInitLib == null) {
        sAppInitLib = appInitLib;
      }
    }
  }
  
  public static synchronized AppInitLib getInstance(){
    if(sAppInitLib==null){
      synchronized (AppInitLib.class){
        if(sAppInitLib==null){
          sAppInitLib=new AppInitLib();
        }
        return sAppInitLib;
      }
    }
    return sAppInitLib;
  }
  
  /**
   * 初化调用，单例时不能调用
   * @param base
   */
  @Override
  public void attachBaseContext(Context base) {
    this.mContext=base;
    setSingletonInstance(this);
  }
  
  @Override
  public void onTerminate(Context context) {
  
  }
  
  @Override
  public void onTrimMemory(int level) {
  
  }
  
  
  public Context getContext(){
    return mContext;
  }
  
}
